package com.wang.controller_wifi.manager;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

import com.wangbl.manager.utils.Logger;

public class SocketController {

	private static final String TAG = "Controller";
	private InputStream in;
	private OutputStream out;
	private Socket socket;

	public boolean connect() {
		Logger.i(TAG, "connect");
		if (socket != null && socket.isConnected()) {
			return true;
		}
		try {
			socket = new Socket("192.168.8.1", 2001);
			in = socket.getInputStream();
			out = socket.getOutputStream();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean write(String msg) {
		Logger.i(TAG, "write:" + msg);
		try {
			if (socket != null && socket.isConnected()) {
				out.write(msg.getBytes());
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public void sendMessage() {
		
	}

	public boolean disconntect() {
		if (socket != null) {
			try {
				socket.close();
				return true;
			} catch (IOException e) {
				e.printStackTrace();
			}
			return false;
		}
		return true;
	}

}
